/*
 * Copyright 2006 Brian Cavalier
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.bc.iterate.examples;

import static org.bc.iterate.Iterate.*;
import org.bc.iterate.Transform;
import org.bc.iterate.io.PrintClosure;

import java.util.regex.Pattern;

public class ByeExample {

	public final static String regex = "leave(\\s+now)?|(good\\s*)?bye|q(uit)?|exit|see(\\s*y(a|ou)(\\s+l(8|ate?)r)?)?|adios(\\s*amig(o|a)s?)|hasta\\s*(la\\s*vista|ma.ana)?|arrivaderci|au.?revoire|ciao|bona\\s*sera|bon\\s*soire?|peace\\s+out(\\s+(yo|g))?";
	public final static Pattern bye = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);

	public static void main(String[] args) {
		try {
			System.out.println("To exit, type something like \"bye\", \"see ya\", etc.");
			until(stream(System.in), grep(bye), chain(
				new Transform<String>() {
					public String apply(String s) { return "You typed: " + s; }
				},
				println()));

/*			for(String s : stream(System.in)) {
				if(bye.matcher(s).matches()) break;
				System.out.println("You typed: " + s);
			}
*/			System.out.println("bye");
		}
		catch (Exception e) {
			System.err.println(e);
		}
	}
}